package com.hl.payServer.controller;

import com.alibaba.fastjson.JSON;
import com.hl.payServer.model.PayProject;
import com.hl.payServer.model.ResultModel;
import com.hl.payServer.model.ResultStatus;
import com.hl.payServer.service.PayProjectService;
import com.hl.payServer.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/project")
public class ProjectController {

    private static final Logger log = LoggerFactory.getLogger(ProjectController.class);
    @Autowired
    private PayProjectService payProjectService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResultModel list(@RequestParam("data") String data, HttpServletRequest request){
        PayProject payProject = null;
        try {
            payProject = JSON.parseObject(data, PayProject.class);
            return ResultModel.ok(payProjectService.queryList(payProject));
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ResultModel save(@RequestParam("data") String data, HttpServletRequest request){
        PayProject payProject = null;
        try {
            payProject = JSON.parseObject(data, PayProject.class);
            Preconditions.checkNotNullAndEmpty(payProject.getProjectCode(), "projectCode");
            Preconditions.checkNotNullAndEmpty(payProject.getProjectName(), "projectName");
            Preconditions.checkNotNullAndEmpty(payProject.getIncomeCompanyCode(), "incomeCompanyCode");
            payProject.initCreateTime();
            return payProjectService.save(payProject);
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResultModel update(@RequestParam("data") String data, HttpServletRequest request){
        PayProject payProject = null;
        try {
            payProject = JSON.parseObject(data, PayProject.class);
            Preconditions.checkNotNullAndEmpty(payProject.getId(), "id");
            Preconditions.checkNotNullAndEmpty(payProject.getProjectCode(), "projectCode");
            Preconditions.checkNotNullAndEmpty(payProject.getProjectName(), "projectName");
            Preconditions.checkNotNullAndEmpty(payProject.getIncomeCompanyCode(), "incomeCompanyCode");
            payProject.setCreateTime(null);
            payProject.initUpdateTime();
            return payProjectService.update(payProject);
        }catch (IllegalArgumentException illegalArgumentException){
            log.error("支付参数："+data+" 参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
            return ResultModel.error(ResultStatus.CLIENT_ERROR,"参数错误:参数不符合验证规则,error:"+illegalArgumentException.getMessage());
        } catch (Exception e) {
            log.error("支付参数:"+data+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResultModel delete(@RequestParam("id") String id, HttpServletRequest request){
        try {
            return payProjectService.delete(id);
        } catch (Exception e) {
            log.error("支付参数:"+id+"系统错误:error:"+e.getMessage(), e);
            return ResultModel.error(ResultStatus.SERVER_ERROR,"系统错误:error:"+e.getMessage());
        }
    }
}
